package org.openfans.rss;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.openfans.domain.Article;
import org.openfans.domain.Facade;
import org.openfans.domain.RootTag;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;

public class RssWriter {
	private Facade facade;

	public void setFacade(Facade facade) {
		this.facade = facade;
	}

	public SyndFeed getFeed(String type) {
		SyndFeed feed = new SyndFeedImpl();
		feed.setFeedType(type);

		feed.setTitle("OpenFans");
		feed.setLink("http://www.openfans.net");
		feed.setDescription("openfans首页Rss");

		List entries = new ArrayList();
		SyndEntry entry;
		SyndContent description;

		List articleList = facade.getRecentArticles(5, "submitDate desc",
				"News");
		for (Iterator iter = articleList.iterator(); iter.hasNext();) {
			Article article = (Article) iter.next();
			entry = new SyndEntryImpl();
			entry.setTitle("最新软件新闻--" + article.getTitle());
			entry.setLink("http://www.openfans.net/viewArticle/"
					+ article.getArticleId());
			entry.setAuthor(article.getFans().getNickName());
			entry.setPublishedDate(article.getSubmitDate());
			description = new SyndContentImpl();
			description.setType("text/plain");
			description.setValue(article.getShortContent());
			entry.setDescription(description);
			entries.add(entry);

		}

		articleList = facade.getRecentArticles(5, "submitDate desc",
				"TechArticle");
		for (Iterator iter = articleList.iterator(); iter.hasNext();) {
			Article article = (Article) iter.next();
			entry = new SyndEntryImpl();
			entry.setTitle("最新技术文章--" + article.getTitle());
			entry.setLink("http://www.openfans.net/viewArticle/"
					+ article.getArticleId());
			entry.setAuthor(article.getFans().getNickName());
			entry.setPublishedDate(article.getSubmitDate());
			description = new SyndContentImpl();
			description.setType("text/plain");
			description.setValue(article.getShortContent());
			entry.setDescription(description);
			entries.add(entry);

		}

		List rootTagList = facade.getRecentRootTags(5, "submitDate desc");
		for (Iterator iter = rootTagList.iterator(); iter.hasNext();) {
			RootTag tag = (RootTag) iter.next();
			entry = new SyndEntryImpl();
			entry.setTitle("最新推荐软件--" + tag.getTitle());
			entry.setLink("http://www.openfans.net/viewTag/" + tag.getTagId());
			entry.setAuthor(tag.getFans().getNickName());
			entry.setPublishedDate(tag.getSubmitDate());
			description = new SyndContentImpl();
			description.setType("text/plain");
			description.setValue(tag.getShortIntro());
			entry.setDescription(description);
			entries.add(entry);
		}

		feed.setEntries(entries);

		return feed;
	}
}
